package org.aeon.colorfulplanebattle.flying.thread;

import org.aeon.colorfulplanebattle.flying.Utils;
import org.aeon.colorfulplanebattle.flying.entity.Bullet;
import org.aeon.colorfulplanebattle.flying.entity.EnemyShip;
import org.aeon.colorfulplanebattle.flying.entity.Ship;

import java.util.List;
import java.util.Properties;
import java.util.Random;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/11 12:27
 * @description: 抛洒线程
 */
public class SprinkleThread extends Thread {
    private static final Properties prop;

    static {
        prop = Utils.getProp(SPRINKLE_THREAD);
    }

    private final List<Ship> ships;
    private final List<Bullet> bullets;
    private final Random random = new Random();
    private boolean flag = true;
    private int shipUpperLimit;                     // 敌方子弹数量上限
    private int bulletUpperLimit;                   // 敌机数量上限
    private int bulletsAmount;                      // 当前敌方子弹数量
    private int shipsAmount;                        // 当前敌机数量
    private int bulletProbability;                  // 当前敌方子弹概率
    private int shipsProbability;                   // 当前敌机概率
    private long checkInterval = CHECK_INTERVAL;    // 检测频率
    private double sizeCoeff;
    private double speedCoeffS = 2;
    private double speedCoeffB = 2;

    public SprinkleThread(List<Ship> ships, List<Bullet> bullets) {
        this.setName("投放线程");
        this.ships = ships;
        this.bullets = bullets;

        Utils.stuffing(prop, this);
    }

    @Override
    public void run() {
        while (flag) {
            // 投放敌方飞机
            if (shipsAmount < shipUpperLimit) {
                shipDrop();
            }
            // 投放敌方子弹
            if (bulletsAmount < bulletUpperLimit) {
                bulletDrop();
            }
            // 间隔
            try {
                Thread.sleep(checkInterval);
            } catch (InterruptedException e) {
            }
        }
    }

    // 投放敌方子弹
    private void bulletDrop() {
        if (bulletProbability < 0) {
            return;
        }
        if (bulletsAmount >= 1) {
            if (bulletProbability > 0) {
                if (random.nextInt(bulletProbability) != 0) return;
            }
        }
        Bullet bullet = new Bullet(ENEMY_CAMP, random.nextInt(1, (int) (2 * speedCoeffB)));
        bullet.setX(random.nextInt(BATTLEFIELD_WIDTH - bullet.getWidth()));
        bullet.setY(-bullet.getHeight());
        bullets.add(bullet);
        System.out.println("投放一个子弹...");
        bulletsAmount++;
    }

    // 投放敌方飞机
    private void shipDrop() {
        if (shipsProbability < 0) {
            return;
        }
        if (shipsAmount >= 2) {
            if (shipsProbability > 0) {
                if (random.nextInt(shipsProbability) != 0) return;
            }
        }
        double multiplier = Math.random() * 0.2 + 1 - sizeCoeff;
        Ship ship = new EnemyShip();
        ship.setWidth((int) (ship.getWidth() * multiplier));
        ship.setHeight((int) (ship.getHeight() * multiplier));
        ship.setTouchWidth((int) (ship.getTouchWidth() * multiplier));
        ship.setTouchHeight((int) (ship.getTouchHeight() * multiplier));
        ship.setX(random.nextInt(BATTLEFIELD_WIDTH - ship.getWidth()));
        ship.setY(-ship.getHeight());
        ship.setSpeed(random.nextInt(1, (int) (ship.getSpeed() * speedCoeffS)));
        ships.add(ship);
        System.out.println("投放一个飞机,颜色为:" + ship.getColor() + "...");
        shipsAmount++;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public int getShipUpperLimit() {
        return shipUpperLimit;
    }

    public void setShipUpperLimit(int shipUpperLimit) {
        this.shipUpperLimit = shipUpperLimit;
    }

    public int getBulletUpperLimit() {
        return bulletUpperLimit;
    }

    public void setBulletUpperLimit(int bulletUpperLimit) {
        this.bulletUpperLimit = bulletUpperLimit;
    }

    public Long getCheckInterval() {
        return checkInterval;
    }

    public void setCheckInterval(Long checkInterval) {
        this.checkInterval = checkInterval;
    }

    public void setCheckInterval(long checkInterval) {
        this.checkInterval = checkInterval;
    }

    public int getBulletsAmount() {
        return bulletsAmount;
    }

    public void setBulletsAmount(int bulletsAmount) {
        this.bulletsAmount = bulletsAmount;
    }

    public int getShipsAmount() {
        return shipsAmount;
    }

    public void setShipsAmount(int shipsAmount) {
        this.shipsAmount = shipsAmount;
    }

    public int getBulletProbability() {
        return bulletProbability;
    }

    public void setBulletProbability(int bulletProbability) {
        this.bulletProbability = Math.max(bulletProbability, 0);
    }

    public int getShipsProbability() {
        return shipsProbability;
    }

    public void setShipsProbability(int shipsProbability) {
        this.shipsProbability = Math.max(shipsProbability, 0);
    }

    public double getSizeCoeff() {
        return sizeCoeff;
    }

    public void setSizeCoeff(double sizeCoeff) {
        this.sizeCoeff = Math.min(sizeCoeff, 0.7);
    }

    public double getSpeedCoeffS() {
        return speedCoeffS;
    }

    public void setSpeedCoeffS(double speedCoeffS) {
        this.speedCoeffS = speedCoeffS;
    }

    public double getSpeedCoeffB() {
        return speedCoeffB;
    }

    public void setSpeedCoeffB(double speedCoeffB) {
        this.speedCoeffB = speedCoeffB;
    }
}
